<?php

/**
	Controller Creted By - Mandar Kulkarni
	Dated On - 29-06-2012
*/

class SecondRegisterController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/main';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model = new EduSchool();
		$modelSchool = new School();
		
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['EduSchool'])){
			$model->attributes=$_POST['EduSchool'];
			
			if(empty($_POST['EduSchool_schoolname_hidden'])){
				$modelSchool->schoolname = $_POST['EduSchool']['schoolname'];
				$modelSchool->save();
				$model->schoolID = $modelSchool->schoolID;
			}
			else
				$model->schoolID = $_POST['EduSchool_schoolname_hidden'];
			
			$model->userID = Yii::app()->session['userID'];
			
			if($model->save()){
				$this->redirect(array('collegeInfo'));
			}	
		}

		$this->render('create',array(
			'model'=>$model,'modelSchool'=>$modelSchool
		));
	}
	
	public function actionCollegeInfo()
	{
		$model = new EduJrcollege();
		$modelCollege = new College();
		
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['EduJrcollege'])){
			$model->attributes = $_POST['EduJrcollege'];
			
			if(empty($_POST['EduJrcollege_college_name_hidden'])){
				$modelCollege->college_name = $_POST['EduJrcollege']['college_name'];
				$modelCollege->save();
				$model->collegeID = $modelCollege->collegeID;
			}
			else
				$model->collegeID = $_POST['EduJrcollege_college_name_hidden'];
			
			$model->userID = Yii::app()->session['userID'];
			
			if($model->save()){
				$this->redirect(array('highQual'));
			}	
		}

		$this->render('jr_college_form',array(
			'model'=>$model,'modelCollege'=>$modelCollege
		));
	}
	
	public function actionHighQual()
	{
		$model= new EduHigherQual();
		$modelCollege = new College();
		
		if(isset($_POST['EduHigherQual'])){ 
			$model->attributes=$_POST['EduHigherQual'];
			
			if(empty($_POST['EduHigherQual_college_name_hidden'])){
				$modelCollege->college_name = $_POST['EduHigherQual']['college_name'];
				$modelCollege->save();
				$model->collegeID = $modelCollege->collegeID;
			}
			else
				$model->collegeID = $_POST['EduHigherQual_college_name_hidden'];
			
			$model->userID = Yii::app()->session['userID'];
			
			if($model->save())
			$this->redirect(array('highQual'));
		}
			
			$this->render('higher_qualification_form',array('model'=>$model));
	}
	
	public function actionEduAdditionalQualification()
	{
		$modelAddCourse= new EduAddCourse();
		$modelExtraActivity= new ExtraActivity();
		
		if(isset($_POST['EduAddCourse']) || isset($_POST['ExtraActivity']))
		{ 
			$modelAddCourse->attributes=$_POST['EduAddCourse'];
			$modelExtraActivity->attributes=$_POST['ExtraActivity'];
			
			if(!empty($_POST['EduAddCourse']['certificate'])){
				$certificate = implode(',',$_POST['EduAddCourse']['certificate']);
				$modelAddCourse->certificate = $certificate;
			}
			
			if(!empty($_POST['EduAddCourse']['description'])){
				$description = implode(',',$_POST['EduAddCourse']['description']);
				$modelAddCourse->description = $description;
			}
			
			if(!empty($_POST['EduAddCourse']['institute_name'])){
				$institute_name = implode(',',$_POST['EduAddCourse']['institute_name']);
				$modelAddCourse->institute_name = $institute_name;
			}
			
			$modelAddCourse->userID = Yii::app()->session['userID'];
			$modelExtraActivity->userID = Yii::app()->session['userID'];
			
			if($modelAddCourse->save() && $modelExtraActivity->save())
			$this->redirect(array('desireJob'));
		}
			
			$this->render('extraactivity',array('modelAddCourse'=>$modelAddCourse,'modelExtraActivity'=>$modelExtraActivity));
	}
	
	public function actionDesireJob()
	{
		$modelDesJob= new DesiredJob();
		$modelJobDet= new JobOtherDetails();
		
		if(isset($_POST['DesiredJob']) || isset($_POST['JobOtherDetails']))
		{ 
			$modelDesJob->attributes=$_POST['DesiredJob'];
			$modelJobDet->attributes=$_POST['JobOtherDetails'];
			
			$modelDesJob->userID = Yii::app()->session['userID'];
			$modelJobDet->userID = Yii::app()->session['userID'];
			
			if($modelDesJob->save() && $modelJobDet->save())
			$this->redirect(array('workExp'));
		}
			
			$this->render('desire_job',array('modelDesJob'=>$modelDesJob,'modelJobDet'=>$modelJobDet));
	}
	
	public function actionWorkExp()
	{
		$modelWorkExp= new WorkExp();
		
		
		if(isset($_POST['WorkExp']))
		{ 
			$modelWorkExp->attributes=$_POST['WorkExp'];
			$modelWorkExp->userID = Yii::app()->session['userID'];
			
			if($modelWorkExp->save())
			$this->redirect(array('employee'));
		}
			
			$this->render('work_exp_form',array('modelWorkExp'=>$modelWorkExp));	
	}
	
	public function actionEmployee()
	{
		$modelEmp= new Employer();
		
		if(isset($_POST['Employer'])){
			$modelEmp->attributes=$_POST['Employer'];
			$modelEmp->userID = Yii::app()->session['userID'];
			$modelEmp->company_cityID = $_POST['Employer_company_cityID_hidden'];
			
			if($modelEmp->save())
				$this->redirect(array('employee'));
		}
		
		$this->render('employee_form',array('modelEmp'=>$modelEmp));	
	}
	
	public function actionProjDet()
	{
		$model= new ProjDetail();
		
		if(isset($_POST['ProjDetail']))
		{ 
			$model->attributes=$_POST['ProjDetail'];
			
			if(!empty($_POST['ProjDetail']['location_name'])){
				$location = implode(',',$_POST['ProjDetail']['location_name']);
				$model->location_name = $location;
			}
			
			if(!empty($_POST['ProjDetail']['skill_set'])){
				$skill = implode(',',$_POST['ProjDetail']['skill_set']);
				$model->skill_set = $skill;
			}
			
			$model->userID = Yii::app()->session['userID'];
			if($model->save())
			$this->redirect(array('projDet'));
		}
		
			$this->render('proj_det_form',array('model'=>$model));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['EduSchool']))
		{
			$model->attributes=$_POST['EduSchool'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->eshoolID));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('EduSchool');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new EduSchool('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['EduSchool']))
			$model->attributes=$_GET['EduSchool'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=EduSchool::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='edu-school-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	
	public function actionGetLocation()
    {
	     $city= new City();
		 if (!empty($_GET['term'])) 
		 {
		 	$arrFinal = array();
		 	$qterm = $_GET['term'];

			$j=0;
			
			$Criteria = new CDbCriteria();
			$Criteria->condition = "city_name LIKE '%".$qterm."%'";

			$citylist = City::model()->findAll($Criteria);
			foreach($citylist as $city1)
			{
				$arrFinal[$j]['id'] = $city1->cityID;
				$arrFinal[$j]['value'] = $city1->city_name;
				$j++;
			}	
			
			
			echo CJSON::encode($arrFinal); exit;
		  } 
		  else 
		  {
			return false;
		  }
    }
	
	public function actionGetSchoolName()
    {
	    $school= new School();
		if (!empty($_GET['term'])){
			$arrFinal = array();
			$qterm = $_GET['term'];

			$j=0;
		
			$Criteria = new CDbCriteria();
			$Criteria->condition = "schoolname LIKE '%".$qterm."%'";

			$schoollist = School::model()->findAll($Criteria);
			foreach($schoollist as $school1)
			{
				$arrFinal[$j]['id'] = $school1->schoolID;
				$arrFinal[$j]['value'] = $school1->schoolname;
				$j++;
			}	
			
			
			echo CJSON::encode($arrFinal); exit;
		} 
		else{
			return false;
		}
    }
	
	public function actionGetCollegeName()
    {
	    $college= new College();
		if (!empty($_GET['term'])){
			$arrFinal = array();
			$qterm = $_GET['term'];

			$j=0;
		
			$Criteria = new CDbCriteria();
			$Criteria->condition = "college_name LIKE '%".$qterm."%'";

			$collegelist = College::model()->findAll($Criteria);
			foreach($collegelist as $college1)
			{
				$arrFinal[$j]['id'] = $college1->collegeID;
				$arrFinal[$j]['value'] = $college1->college_name;
				$j++;
			}	
			
			
			echo CJSON::encode($arrFinal); exit;
		} 
		else{
			return false;
		}
    }
	
	public function actionSearchCity()
	{
		$arrParam =array();
		if(isset($_POST['DesiredJob']['djob_stateID']))
		{	
			//Get state id for which city is to be search
			$Term = $_POST['DesiredJob']['djob_stateID'] ;
			
			$case = "BY-STATE-ID";
			$prompt = "Select City";
		
			$arrParam['stateID'] = isset($Term)?$Term:null ;
			
			//Get list of cities
			$data = City::getList($case,$arrParam);
			
			//Convert object listing data of cities into array of city list
			$data = CHtml::listData($data,'cityID','city_name');
				
			echo CHtml::tag('option',array('value'=>''),$prompt,true);
			foreach($data as $value=>$name)
			{
				echo CHtml::tag('option',array('value'=>$value),CHtml::encode($name));
			}	
		}
		Yii::app()->end();
	}
}
